package com.jnu.mydiary.ui.shouye1;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.jnu.mydiary.AddActivity;
import com.jnu.mydiary.R;
import com.jnu.mydiary.ShowActivity;
import com.jnu.mydiary.adapter.beanAdapter;

import com.jnu.mydiary.databinding.FragmentDashboardBinding;
import com.jnu.mydiary.ui.shouye1.DashboardViewModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class DashboardFragment extends Fragment  {

    private DashboardViewModel dashboardViewModel;
    private FragmentDashboardBinding binding;
    private ListView mListView;
    private List<myBean> myBeanList=new ArrayList<>();//用来存放数据的数组
    private beanAdapter adapter;
    private  View.OnCreateContextMenuListener mConvListOnCreateContextMenuListener;
    private View view=null;
    public static int num=-1;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        if(null!=view){
            ViewGroup parent=(ViewGroup) view.getParent();
            if(null!=parent){
                parent.removeView(view);
            }
        }
        else {
            dashboardViewModel =
                    new ViewModelProvider(this).get(DashboardViewModel.class);

            binding = FragmentDashboardBinding.inflate(inflater, container, false);
            view = binding.getRoot();

                    mListView = view.findViewById(R.id.home_lv);
                    init();
                    adapter = new beanAdapter(getActivity(), R.layout.layout_home_item, myBeanList);

                    mListView.setAdapter(adapter);
                    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            myBean mBean = myBeanList.get(position);
                            Intent intent = new Intent(getActivity(), ShowActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("DataInfo", mBean);
                            bundle.putString("type", "show");
                            intent.putExtras(bundle);
                            startActivity(intent);

                        }
                    });
                    mConvListOnCreateContextMenuListener = new View.OnCreateContextMenuListener() {

                        @Override
                        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

                            menu.add(0, 0, 0, "删除");
                        }


                    };
                    mListView.setOnCreateContextMenuListener(mConvListOnCreateContextMenuListener);


        }

        return view;
    }

    private void init() {
        DataBank mDataBank=new DataBank(getActivity());
        myBeanList=mDataBank.loadData();

        Bundle bundle = getActivity().getIntent().getExtras();//在这里去处理传过来的数据
        if(bundle!=null) {
            Serializable serializable = bundle.getSerializable("DataInfo");
            if (serializable != null && serializable instanceof myBean) {
                //goodsEntityList.add(new Data("111",20.0,"none",new Date(),R.drawable.home));
                myBean g = (myBean) serializable;
                if(bundle.getString("type").equals("add")&&num==-1)
                {
                    Log.i("type",bundle.getString("type"));
                    int i=bundle.getInt("position");
                    myBeanList.add(g);
                    Collections.sort(myBeanList);
                    mDataBank.saveData(myBeanList);
                    num=0;
                }
            }
        }

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuInfo;
        menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        int index = menuInfo.position;
        Log.i("position",index+"");
        switch (item.getItemId()) {
            case 0:
                DataBank mDataBank=new DataBank(getActivity());
                myBeanList.remove(index);
                mDataBank.saveData(myBeanList);
                adapter.notifyDataSetChanged();
                break;
        }
        return super.onContextItemSelected(item);
    }


}